<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'web_site/web_page/just_headers_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/admin_panel/site_admin_ajax_form_ctrl_with_object_ua.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/forms/_ci/ci_article/ci_article_form_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_attr_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');


class CI_Article_Form_Ajax_Ctrl extends Site_Admin_Ajax_Form_Ctrl_With_Object_UA {
	protected function init_transit_vars() {
		$this->add_transit_var('category');
		$this->add_transit_var('vpage');
	}


	public function run() {
		parent::run();
		$current_user = $this->get_user();
		$this->form_ctrl->set_permanent_external_param('user_id', $current_user->get_id());
	}


	protected function on_good_submit() {
		$object = $this->form_ctrl->get_object();
		$vpage_id = $this->get_transit_var('vpage', true);

		$params_arr = array('manage' => $object->get_id());
		if ($vpage_id) {
			$params_arr['vpage'] = $vpage_id;
		}
		$redir = $this->create_redirect_composer('admin/modules/tangra_cms/ci/ci_article/ci_article_manage.php', $params_arr);

		$location = $redir->get_target_address();

		$ret = array('command' => Ajax_Ctrl::COMMAND_REDIRECT, 'command_value' => $location);

		return $ret;
	}


	protected function on_bad_submit() {
		$add_new = $this->form_ctrl->get_permanent_external_param('add_new', true);
		if ($add_new) {
			$this->export('add_new', 1);
			$vpage_id = $this->get_transit_var('vpage');
			if ($vpage_id) {
				$this->export('vpage_id', $vpage_id);
				$this->form_ctrl->set_permanent_external_param('vpage', $vpage_id);
			}
		} else {
			$object = $this->form_ctrl->get_object();
			$this->export('ci_id', $object->get_id());
		}

		return true;
	}


	protected function create_form_ctrl() {
		$tvm = $this->get_tvm();
		$dbc = $this->get_dbc();


		$type = new TCMS_CI_Type_DBC();
		if (!$type->load_by_hid($dbc, 'article')) {
			throw new Tangra_Module_Exception('Cannot load TCMS_CI_Type_DBC with hid = "article"');
		}

		$attribures = TCMS_CI_Type_Attr_DBC::select_all_for_ci_type($dbc, $type->get_id());
		$ret = new CI_Article_Form_Ctrl('ci_article', $tvm, $dbc, 'obj_id', $attribures);

		return $ret;
	}

}